﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;

namespace UE.SharePoint.ImportExport.BLL.Excel
{
    /// <summary>
    /// Represents a writer that use to export data to xls file
    /// </summary>
    public class XLSWriter : IWritter
    {
        /// <summary>
        /// export status
        /// </summary>
        public IStatus Status { get; set; }

        /// <summary>
        /// setting
        /// </summary>
        public Setting Setting { get; set; }

        /// <summary>
        /// ctor
        /// </summary>
        public XLSWriter(Setting setting, IStatus status)
        {
            this.Setting = setting;
            this.Status = status; 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="added"></param>
        /// <param name="updated"></param>
        /// <returns></returns>
        public bool Writer(DataTable added, DataTable updated = null)
        {
            MemoryStream stream = new MemoryStream();
            HSSFWorkbook xls = Processed(added);
            xls.Write(stream);
            Setting.ResponseMemory = stream;
            return true;
        }

        /// <summary>
        /// Exports to .xls
        /// </summary>
        /// <param name="exportData"></param>
        /// <returns></returns>
        private HSSFWorkbook Processed(DataTable exportData)
        {
            HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
            string sheetName = exportData.TableName;
            Sheet workSheet = hSSFWorkbook.CreateSheet(sheetName);
            int rowIndex = 0;
            int columnIndex = 0;

            //add columns
            CellStyle titleStyle = hSSFWorkbook.CreateCellStyle();
            titleStyle.FillBackgroundColor = HSSFColor.LIGHT_BLUE.BLUE_GREY.index;
            titleStyle.FillForegroundColor = HSSFColor.WHITE.index;
            Row headRow = workSheet.CreateRow(rowIndex++);
            foreach (DataColumn column in exportData.Columns)
            {
               Cell cell =  headRow.CreateCell(columnIndex++);
               cell.SetCellValue(column.ColumnName);
               cell.CellStyle = titleStyle;
            }

            foreach (DataRow row in exportData.Rows)
            {
                Row currentRow = workSheet.CreateRow(rowIndex++);
                columnIndex = 0;
                for (int i = 0; i < exportData.Columns.Count; i++)
                {
                    Cell cell = currentRow.CreateCell(columnIndex++);
                    cell.SetCellValue(row[i].ToString());
                }
            }

            //auto set size 
            for (int i = 0; i < exportData.Columns.Count; i++)
            {
                workSheet.AutoSizeColumn(i);   
            }

            return hSSFWorkbook;
        }
    }
}
